package com.weimei.conversionTreasure.viewModel;

import android.app.Application;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableField;

import com.weimei.conversionTreasure.activity.VipActivity;
import com.weimei.conversionTreasure.http.Repository;
import com.weimei.conversionTreasure.http.entity.BaseResult;
import com.weimei.conversionTreasure.utils.UserUtils;

import java.util.HashMap;
import java.util.Map;

import io.reactivex.observers.DisposableObserver;
import me.goldze.mvvmhabit.base.BaseViewModel;
import me.goldze.mvvmhabit.utils.RxUtils;
import me.goldze.mvvmhabit.utils.ToastUtils;

public class AddPfVM extends BaseViewModel<Repository> {
    public AddPfVM(@NonNull Application application, Repository model) {
        super(application, model);
    }


    public ObservableField<String> sid = new ObservableField<>("");


    public void pulish(String title, String content) {
        if (!UserUtils.isVip()){
            ToastUtils.showShort("您还不是会员，请先开通会员后再使用！！！");
            startActivity(VipActivity.class);
            return;
        }

        Map<String, String> map = new HashMap<>();
        map.put("sid", sid.get());
        map.put("user_id", UserUtils.getUserAccount());
        map.put("title", title);
        map.put("content", content);

        model.addformula(map)
                .compose(RxUtils.schedulersTransformer())
                .compose(RxUtils.exceptionTransformer())
                .doOnSubscribe(this)
                .doOnSubscribe(disposable -> {
                })
                .subscribe(new DisposableObserver<BaseResult>() {
                    @Override
                    public void onNext(BaseResult baseResult) {
                        if (baseResult.issucc) {
                            ToastUtils.showShort(baseResult.msg);
                            finish();

                        }
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }
}
